package com.itheima.reggie.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
        implements SetmealService
{
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishService setmealDishService;



    @Override
    public Result<JSONObject> findPage(Integer page, Integer pageSize, String name) {

        page = (page-1)*pageSize;
        JSONObject json = new JSONObject();
        json.put("total",setmealMapper.findCount(name));
        json.put("records",setmealMapper.findPage(page,pageSize,name));
        return Result.success(json);
    }

    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     * @param setmealDto
     */
    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，操作setmeal，执行insert操作
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            item.setCreateTime(setmealDto.getCreateTime());
            item.setCreateUser(setmealDto.getCreateUser());
            item.setUpdateTime(setmealDto.getUpdateTime());
            item.setUpdateUser(setmealDto.getUpdateUser());
            return item;
        }).collect(Collectors.toList());

        //保存套餐和菜品的关联信息，操作setmeal_dish,执行insert操作
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public Result<String> delete(List<Long> ids,Long user) {

        LambdaQueryWrapper<Setmeal> countQueryWrapper = new LambdaQueryWrapper();
        countQueryWrapper.in(Setmeal::getId,ids);
        countQueryWrapper.eq(Setmeal::getStatus,1);

        int count = count(countQueryWrapper);
        if(count>0){
            return Result.error("有套餐在起售，不给删除");
        }
        LocalDateTime now = LocalDateTime.now();
        setmealDishService.deleteBySetmealIds(ids);

        setmealMapper.removeIds(ids,now,user);
        return Result.success("删除成功");
    }

    @Override
    public void editStatus(Integer status, List<Long> ids,Long user) {
        setmealMapper.editStatus(status,ids,LocalDateTime.now(),user);
    }

    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //更新dish表基本信息
        this.updateById(setmealDto);

        List<Long> setmeals = new ArrayList<>();
        setmeals.add(setmealDto.getId());

        setmealDishService.deleteBySetmealIds(setmeals);

        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            item.setUpdateTime(setmealDto.getUpdateTime());
            item.setUpdateUser(setmealDto.getUpdateUser());
            item.setCreateTime(setmealDto.getCreateTime());
            item.setCreateUser(setmealDto.getUpdateUser());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //查询菜品基本信息，从dish表查询
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(this.getById(id),setmealDto);

        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(setmealDishes);

        return setmealDto;
    }

    @Override
    public List<JSONObject> selectEachAnalysis() {
        return setmealMapper.selectEachAnalysis();
    }
}